<?php
class AdminBill extends CActiveRecord{

    public static function model($className = __CLASS__) {
        return parent::model ( $className );
    }

    // dat luat cho model
    public function rules(){}         

    // goi den bang can ket noi   
    public function tableName() {                  
        return 'bill';
    }

    // nhan cac thuong tinh 
    public function attributeLabels(){}

    public function getRowById($id){
        $id = intval($id);
        $row = array();

        $sql = "SELECT * FROM bill WHERE id=".$id;
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();

        return $row;
    }
    
    public function countBill($province_id,$keyword,$type_keyword,$service_id,$tungay,$denngay){        
        $str .= " user_name LIKE '%$keyword%'";        
        if($province_id != 0){
            $str .= " AND province_id = '$province_id'";    
        }
        if($service_id != ""){
            $str .= " AND bill_type = '$service_id'";    
        }
        if($tungay != ""){
            $str .= " AND create_date > '$tungay'";    
        }
        if($denngay != ""){
            $str .= " AND create_date < '$denngay'";    
        }
        $sql = "SELECT count(id) as count FROM bill WHERE ".$str;        
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();
        $result = $row['count'];
        return $result; 
    }

    public function searchBill($province_id,$keyword,$type_keyword,$service_id,$tungay,$denngay,$begin,$end){
        $str .= " user_name LIKE '%$keyword%'";        
        if($province_id != 0){
            $str .= " AND province_id = '$province_id'";    
        }
        if($service_id != ""){
            $str .= " AND bill_type = '$service_id'";    
        }
        if($tungay != ""){
            $str .= " AND create_date > '$tungay'";    
        }
        if($denngay != ""){
            $str .= " AND create_date < '$denngay'";    
        }
        $str .= " ORDER BY id DESC LIMIT ".$begin.",".$end;
        $sql = "SELECT * FROM bill WHERE ".$str;        
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $rows = $command->queryAll();

        return $rows;
    }        

    public function getAllRows(){
        //$id_region = intval($id_region);

        $rows = array();

        $sql = "SELECT * FROM bill order by id desc";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $rows = $command->queryAll();

        return $rows;
    } 
}
